package com.yuji.aacar.net.convertfactory;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.yuji.aacar.net.annotaion.BASE64;
import com.yuji.aacar.net.annotaion.CODE;
import com.yuji.aacar.net.annotaion.NOSECRET;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * <li>Package:com.ttp.http_core </li>
 * <li>Author: yhj </li>
 * <li>Date: 2017/2/15 </li>
 * <li>Description: retrofit转换器工厂类 </li>
 */

public class JsonConverterFactory extends Converter.Factory {

    public static JsonConverterFactory create() {
        return create(new Gson());
    }

    /**
     * Create an instance using {@code gson} for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     */
    public static JsonConverterFactory create(Gson gson) {
        return new JsonConverterFactory(gson);
    }

    private final Gson gson;

    private JsonConverterFactory(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        this.gson = gson;
    }

    /**
     * 返回转换器
     * @param type 参数类型
     * @param annotations 注解
     * @param retrofit retrofit对象
     * @return
     */
    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {

        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));

        if(annotations!=null &&annotations.length>0){
            for (Annotation annotation:annotations){
                if(annotation instanceof NOSECRET){
                    return new JsonNoSecretResponseConverter<>(gson, adapter);
                }else if (annotation instanceof BASE64){
                    return new JsonBase64ResponseConverter<>(gson,adapter);
                }
            }
        }
        return new JsonResponseBodyConverter<>(gson, adapter);  //响应
    }

    /**
     * 请求转换器
     * @param type 参数类型
     * @param parameterAnnotations 参数注解
     * @param methodAnnotations 方法注解
     * @param retrofit retrofit对象
     * @return
     */
    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {

        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));

        int code=0;
        if(methodAnnotations!=null&&methodAnnotations.length>0) {
            for (Annotation annotation : methodAnnotations) {
                if (annotation instanceof CODE) {
                    code = ((CODE) annotation).value();
                }
                if (annotation instanceof NOSECRET) {
                    return new JsonNoSecretRequestConverter(gson, adapter, code);
                }else if(annotation instanceof BASE64){
                    return new JsonBase64RequestConverter<>(gson, adapter, code);
                }
            }
        }
        return new JsonRequestBodyConverter<>(gson, adapter,code); //请求
    }
}